package _15_generics._15_2.exercise;

/**
 * Remove the type parameter on the Node class and
 * modify the rest of the code in the LinkedList.java
 * to show that an inner has access to the generic type
 * parameters of its outer class
 */
public class E05_LinkedStack2 {
    public static void main(String[] args) {
        LinkedStack2<String> stack = new LinkedStack2<String>();
        stack.push("hello");
        stack.push(",");
        stack.push("world");
        String pop = null;
        while ((pop = stack.pop()) != null){
            System.out.println(pop);
        }
    }
}

class LinkedStack2<T> {
    class Node {
        T item;
        Node next;
        Node(){
            this.item = null;
            this.next = null;
        }
        Node(T item,Node next){
            this.item = item;
            this.next = next;
        }
        boolean end(){
            return this.item == null && this.next == null;
        }
    }

    private Node top = new Node();

    public void push(T item){
        top = new Node(item,top);
    }
    public T pop(){
        T item = top.item;
        if (item != null){
            top = top.next;
        }
        return item;
    }
}
